<script setup lang="ts">
import { getLogisticsInfo } from '@/apis/order'
import router from '@/router'
import type { Logistics, Location } from '@/types/order'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import AMapLoader from "@amap/amap-jsapi-loader"
//引入地图标记图片
import startImg from '@/assets/start.png'
import endImg from '@/assets/end.png'
import carImg from '@/assets/car.png'
window._AMapSecurityConfig = {
  //密钥
  securityJsCode: "2c4393f1207e30f4944a620e491dc95f",
}
//初始化地图
const initMap = () => {
  AMapLoader.load({
    key: "050620993dc22cb92d9ffea186842de6", // 申请好的Web端开发者Key，首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  })
    .then((AMap) => {
      const map = new AMap.Map('map', {
        //自定义地图显示样式
        mapStyle: 'amap://styles/whitesmoke',
        //地图缩放
        zoom: 12,
      })
      AMap.plugin("AMap.Driving", function () {
        const driving = new AMap.Driving({
          map,
          showTraffic: false,// 设置是否显示实时路况信息，默认设置为true。 显示绿色代表畅通，黄色代表轻微拥堵，红色代表比较拥堵，灰色表示无路况信息。
          hideMarkers: true// 设置隐藏路径规划的起始点图标 设置为true：隐藏图标；设置false：显示图标\n默认值为：false
        })
        if (logisticsData.value?.logisticsInfo) {
          const list = [...logisticsData.value.logisticsInfo]
          const startLngLat = list.shift()  //起始点坐标

          /**
         * 创建标记
         * @param point 标记的位置
         * @param image 标记的图片地址
         * @param width 图片的宽度
         * @param height 图片的高度
         * @returns 创建后的标记
         */
          const mapMarker = (image: string, point?: Location, width: number = 25, height: number = 30) => {
            //创建 AMap.Icon 实例：
            const icon = new AMap.Icon({
              size: new AMap.Size(width, height), //图标尺寸
              image: image, //Icon 的图像
              imageSize: new AMap.Size(width, height), //根据所设置的大小拉伸或压缩图片
            });
            // 将 Icon 实例添加到 marker 上:
            const marker = new AMap.Marker({
              position: new AMap.LngLat(point?.longitude, point?.latitude), //点标记的位置
              offset: new AMap.Pixel(-width / 2, -height / 2), //偏移量
              icon: icon, //添加 Icon 实例
            })
            return marker
          }
          // 创建开始标记
          const startMarker = mapMarker(startImg, startLngLat)
          //添加开始标记
          map.add(startMarker)
          const endLngLat = list.pop() //终点坐标
          // 创建结束标记
          const endMarker = mapMarker(endImg, endLngLat)
          //添加结束标记
          map.add(endMarker)
          //使用经纬度方式添加途径点
          const opts = {
            waypoints: list.map(item => [item.longitude, item.latitude]), //途经点参数，最多支持传入16个途经点
          }
          driving.search([startLngLat?.longitude, startLngLat?.latitude], [endLngLat?.longitude, endLngLat?.latitude], opts, () => {
            // 获取当前的运输位置
            const currentPoint = mapMarker(carImg, logisticsData.value?.currentLocationInfo)
            map.add(currentPoint)
            // 3秒后将当前运输的位置定位到中间同时需要进行一定的缩放
            setTimeout(() => {
              map.setFitView([currentPoint]),
                map.setZoom(12)
            }, 3000)
          })
        }
      })
    })
    .catch((e) => {
      console.log(e)
    })
}

//保存物流信息
const logisticsData = ref<Logistics>()
const route = useRoute()
onMounted(async () => {
  const res = await getLogisticsInfo(route.params.id as string)
  console.log(res)
  logisticsData.value = res.data
  initMap()
})
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="router.back()" />
        <span>{{ logisticsData?.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">订单{{ logisticsData?.statusValue }} 预计{{ logisticsData?.estimatedTime }}送达</p>
        <p class="predict">
          <span>{{ logisticsData?.name }}</span>
          <span>{{ logisticsData?.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logisticsData?.list" :key="item.id">
          <p class="status" v-if="item.statusValue">{{ item.statusValue }}</p>
          <p class="content">{{ item.content }}</p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}

#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;

  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;

    >span {
      flex: 1;
      text-align: center;
    }

    .van-icon {
      font-size: 18px;
      color: #666;

      &:last-child {
        color: var(--cp-primary);
      }
    }
  }

  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;

    .status {
      font-size: 15px;
      line-height: 26px;
    }

    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;

      >span {
        padding-right: 10px;
      }
    }
  }
}

.logistics {
  padding: 0 10px 20px;

  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }

  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }

    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }

    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }

    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
